//
//  MJRefreshNormalHeader.swift
//  OC-Swift
//
//  Created by 陆文宇 on 2017/2/14.
//  Copyright © 2017年 陆文宇. All rights reserved.
//

import UIKit

class MJRefreshNormalHeader: MJRefreshStateHeader {

    var activityIndicatorViewStyle: UIActivityIndicatorViewStyle = .gray {
        didSet {
            loadingView.activityIndicatorViewStyle = activityIndicatorViewStyle
            loadingView.removeFromSuperview()
            addSubview(loadingView)
            setNeedsLayout()
        }
    }
    lazy var arrowView: UIImageView = UIImageView(image: Bundle.arrowImage)
    lazy var loadingView: UIActivityIndicatorView = {
        let _loadingView: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .gray)
        _loadingView.hidesWhenStopped = true
        return _loadingView
    }()
    override var state: MJRefreshComponent.MJRefreshState {
        set {
            let oldState: MJRefreshState = super.state
            if super.state != newValue {
                super.state = newValue
            }
            
            if state == .idle {
                if oldState == .refreshing {
                    self.arrowView.transform = CGAffineTransform.identity
                    
                    UIView.animate(withDuration: MJRefreshSlowAnimationDuration, animations: {
                        self.loadingView.alpha = 0
                    }, completion: { (finished) in
                        if self.state != .idle {
                            return
                        }
                        
                        self.loadingView.alpha = 1
                        self.loadingView.stopAnimating()
                        self.arrowView.isHidden = false
                    })
                } else {
                    loadingView.stopAnimating()
                    arrowView.isHidden = false
                    UIView.animate(withDuration: MJRefreshFastAnimationDuration, animations: {
                        self.arrowView.transform = CGAffineTransform.identity
                    })
                }
            } else if state == .pulling {
                loadingView.stopAnimating()
                arrowView.isHidden = false
                UIView.animate(withDuration: MJRefreshFastAnimationDuration, animations: {
                    self.arrowView.transform = CGAffineTransform(rotationAngle: 0.000001 - CGFloat.pi)
                })
            } else if state == .refreshing {
                loadingView.alpha = 1.0
                loadingView.startAnimating()
                arrowView.isHidden = true
            }
        }
        
        get {
            return _state
        }
    }
    override init(refreshingClosure: @escaping MJRefreshComponentRefreshingClosure) {
        super.init(refreshingClosure: refreshingClosure)
        
        addSubview(arrowView)
        addSubview(loadingView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func prepare() {
        super.prepare()
    }
    
    override func placeSubviews() {
        super.placeSubviews()
        
        var arrowCenterX: CGFloat = mj_w * 0.5
        if !stateLabel.isHidden {
            let stateWidth: CGFloat = stateLabel.mj_textWith
            var timeWidth: CGFloat = 0
            if !lastUpdatedTimeLabel.isHidden {
                timeWidth = lastUpdatedTimeLabel.mj_textWith
            }
            let textWidth: CGFloat = max(stateWidth, timeWidth)
            arrowCenterX -= textWidth / 2 + labelLeftInset!
        }
        let arrowCenterY: CGFloat = mj_h * 0.5
        let arrowCenter: CGPoint = CGPoint(x: arrowCenterX, y: arrowCenterY)
        
        if arrowView.constraints.count == 0 {
            arrowView.mj_size = arrowView.image!.size
            arrowView.center = arrowCenter
        }
        
        if loadingView.constraints.count == 0 {
            loadingView.center = arrowCenter
        }
        
        arrowView.tintColor = stateLabel.textColor
    }
    
}
